

package com.easy.controller;

import com.easy.bean.Department;
import com.easy.bean.Employee;
import com.easy.service.DepartmentService;
import com.easy.service.EmployeeService;
import com.easy.util.Page;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("/api/employee")
 // 修改路径为 employee
public class EmployeeController {  // 类名修改为 EmployeeController

    @Autowired
    EmployeeService employeeService;

    // 新增员工
    @PostMapping
    public Result add(@RequestBody Employee employee) {
        return Result.success(employeeService.add(employee));
    }

    // 编辑员工
    @PutMapping("/{id}")
    public Result edit(@PathVariable("id") Integer id, @RequestBody Employee employee) {
        employee.setId(id);
        return Result.success(employeeService.edit(employee));
    }

    // 删除员工
    @DeleteMapping("/{id}")
    public Result del(@PathVariable("id") Integer id) {
        return Result.success("删除数据",employeeService.del(id));
    }

    // 查询所有员工（可模糊查询）
    @GetMapping
    public Result all(
            @RequestParam(name = "name", required = false, defaultValue = "") String name,
            @RequestParam(name = "phone", required = false, defaultValue = "") String phone
    ) {
        return Result.success(employeeService.all(name, phone));
    }

    // 查询单个员工
    @GetMapping("/{id}")
    public Result one(@PathVariable("id") Integer id) {
        return Result.success(employeeService.one(id));
    }

    // 分页查询员工
    @GetMapping("page")
    public Result page(Page page, Employee employee){
        employeeService.page(page, employee);
        return Result.success(page);
    }
}
